#include "MessageParser.h"
#include "P2PChat.h"
/**
 * Init the null instance for the singletone controller
 */
MessageParser* MessageParser::instance = NULL;

MessageParser::MessageParser() : QObject()
{
	fullDatagramRegExp = new QRegExp("<[^>]*>");
}

/**
 * Singletone get instance
 */
MessageParser* MessageParser::getInstance()
{
	if(instance == NULL)
	{
		instance = new MessageParser();
	}
	
	return instance;
}

/**
 * Parse a string
 */
void MessageParser::parseString(QByteArray *input, QHostAddress address)
{
	// temp stuff
	int pos = 0;
	QStringList l;
	
	while((pos = fullDatagramRegExp->indexIn(*input, pos)) != -1)
	{
		l << fullDatagramRegExp->capturedTexts();
		pos += fullDatagramRegExp->matchedLength();
	}

	/* Clear the list entries because these are rounded by '<' and '>' */
	l.replaceInStrings(QRegExp("<|>"), "");
	
	P2PChat::getInstance()->getCommands()->value("alive")->executeCommand(&l, address);
	
}
